//
//  main.cpp
//  Practice
//
//  Created by 赵世杰 on 2020/6/21.
//  Copyright © 2020 zhaoshijie. All rights reserved.
//

#include <iostream>
#include <cctype>
#include <array>
#include <string>
#include <fstream>
#include <vector>



using namespace std;

struct Bop {
    
    char fullname[100];
    char title[100];
    char boname[100];
    int preferrence;
    
};


struct Donation
{
    char name[100];
    
    double money;
};


int main(int argc, const char * argv[]) {
    // insert code here...
    std::cout << "Hello, World!\n";
    
    

    /*
    cout << "请输入字符串" << endl;
    
    char ch ;
    
    cin.get(ch);
    
    while (ch != '@' && ch != '\n') {
        
    
        
        if(isalpha(ch))
        {

            if(isupper(ch))
            {
                cout << (char)tolower(ch) << " ";
                
            }
            else
            {
                cout << ch << " ";
            }
        }
        else
        {
            cout << " 数字 ";
        }
        
        
        cin.get(ch);
        
    }
    
    
    cout << endl;
    */
    
    
    /*
    array<double, 100> donation;
    
    cout << "输入 donation" << endl;
    
    int index = 0 ;
    
    double total = 0;
    
    while (cin >> donation[index]) {
        
        total += donation[index];
        
        index ++;
        
    }
    
    cout << "结束输入，开始计算" << endl;
    
    double avg = total/index;
    
    cout << "平均值为：" << avg << endl;
    
    for (int i=0; i<index; i++) {
        
        if(donation[i] > avg)
        {
            cout << "第" << i << "个donation大于平均值：" << donation[i] << endl;
        }
        
    }
     */
    
    
    /*
    cout << "Please enter one of the following choices:" <<endl;
    
    cout << "c) carnivore      " << "p) pianist  " << endl;
    cout << "t) tree           " << "g) game" << endl;
    
    
    char ch ;
    
    cin.get(ch).get();
    
    
    if(isalpha(ch))
    {

        if(isupper(ch))
        {
            ch = (char)tolower(ch);
            
        }
    }
    

    while (1) {
        
        if(ch == 'c' || ch == 'p' || ch == 't' || ch == 'g' || ch == '\n')
        {

            switch (ch) {
                case 'c':
                {
                    cout << "A maple is a carnivore." << endl;
                }
                    break;
                case 'p':
                {
                    cout << "A maple is a pianist." << endl;
                }
                    break;
                case 't':
                {
                    cout << "A maple is a tree." << endl;
                }
                    break;
                case 'g':
                {
                    cout << "A maple is a game." << endl;
                }
                    break;
                    
                default:
                    break;
            }
            
            break;
        }
        else
        {
            cout << "Please enter a c,p,t or g : " << ch << endl;
            
            cin.get(ch).get();
            

            if(isalpha(ch))
            {

                if(isupper(ch))
                {
                    ch = (char)tolower(ch);
                    
                }
            }
            
        }
        
        
    }
     */
    
    /*

     char fullname[100];
     char title[100];
     char boname[100];
     int preferrence;
     
     */
    
    
    /*
    string fullname = "fullname";
    string title = "title";
    string boname = "boname";
    int preferrence = 0;
    
    array<Bop, 3> bopArray ;
    
    for (int i=0; i<3; i++) {
        fullname = fullname + to_string(i);
        title = title + to_string(i);
        boname = boname + to_string(i);
        preferrence = i;
        
//        Bop bop = {(char*)fullname.c_str(),(char*)title.c_str(),(char*)boname.c_str(),preferrence};
        
        Bop bop ;
        
        strcpy(bop.fullname,(char*)fullname.c_str());
        strcpy(bop.title,(char*)title.c_str());
        strcpy(bop.boname,(char*)boname.c_str());
        bop.preferrence = preferrence;
        
        bopArray[i] = bop;
        
        
    }
    
    
    
    cout << "Benevolent Order of Programmers Report" <<endl;
    
    cout << "a) display by name      " << "b) display by title  " << endl;
    cout << "c) display bg bopname   " << "d) display by preference" << endl;
    cout << "q) quit" << endl;
    
    
    char ch ;
    
    cin.get(ch).get();
    
    while (1) {
        
        if(ch == 'q')
        {
            cout << "Bye!" << endl;
        }
        else if (ch == 'a')
        {
            for (int i=0; i<3; i++)
            {
                Bop bop = bopArray[i];
                
                cout << bop.fullname << endl;
                
            }
            
        }
        else if (ch == 'b')
        {
            for (int i=0; i<3; i++)
            {
                Bop bop = bopArray[i];
                
                cout << bop.title << endl;
                
            }
            
        }
        else if (ch == 'c')
        {
            for (int i=0; i<3; i++)
            {
                Bop bop = bopArray[i];
                
                cout << bop.boname << endl;
                
            }
        }
        else if (ch == 'd')
        {
            Bop bop = bopArray[0];
            
            int preferrence = bop.preferrence;
            
            if(preferrence == 0)
            {
                ch = 'a';
            }
            else if (preferrence == 1)
            {
                ch = 'b';
            }
            else if (preferrence == 2)
            {
                ch = 'c';
            }
            continue;
            
        }
        
        cout << "Next chlice: ";
        
        cin.get(ch).get();
        
            
        
    }
     */
    
    /*
    cout << "请输入收入" << endl;
    
    double revenue;
    
    
    
    while (cin >> revenue) {
        
        if(revenue<0)
        {
            
            cout << "输入有误："+to_string(revenue);
            
            break;
        }
        
        //  计算
        
        double total = 0;
        
        if(revenue<5000)
        {
            total = 0;
        }
        else if(revenue < 15000)
        {
            total = (revenue-5000)*0.1;
        }
        else if (revenue < 35000)
        {
            total = 10000*0.1 + (revenue-15000)*0.15;
        }
        else if (revenue>3500)
        {
            total = 10000*0.1 + 20000*0.15 + (revenue-35000)*0.20;
        }
        
        cout << "当前薪资为：" << revenue << "元，需要交的税收为：" << total << "元" <<endl;
        
        cout << "请输入收入" << endl;
        
        cin.get();
        cin.clear();
        
    }
     */
    
    
    /*
    array<Donation, 3> donationList;
    
    cout << "输入内容" << endl;
    
    for (int i=0; i<3; i++) {
        
        Donation donation ;
        
        cout << "名称:";
        
        char name[20];
        
        cin.getline(name, 20);
        cin.clear();
        
        strcpy(donation.name, name);
        
        cout << "捐款额:" ;
        
        double money ;
        
        cin >> money;
        
        cin.get();
        cin.clear();
        
        donation.money = money;
        
        donationList[i] = donation;
        
        
    }
    
    
    int count = 0;
    
    cout << "Grand Patrons" << endl;
    
    for (int i=0; i<3; i++) {
        
        Donation donation = donationList[i];
        
        if(donation.money>1000)
        {
            cout << "name:" << donation.name << " money:" << donation.money << endl;
            
            count++;
        }
        
    }
    
    cout << "Patrons" << endl;
    
    
    for (int i=0; i<3; i++) {
        
        Donation donation = donationList[i];
        
        if(donation.money>0 && donation.money<1000)
        {
            cout << "name:" << donation.name << " money:" << donation.money << endl;
            
            count++;
        }
        
    }
    
    if(count == 0)
    {
        cout << "none" << endl;
    }
     */
    
    
    /*
    string fileName = "/Users/zhaoshijie/Desktop/c_learning_practice_demo/第6章/章节末编程练习/Practice/Practice/file.txt";
    
    ifstream inFile ;
    
    inFile.open(fileName);
    
    if(!inFile.is_open())
    {
        cout << "不能打开文件" << endl;
        
        exit(EXIT_FAILURE);
    }
    
    string value ;
    
    inFile >> value;
    
    cout << value;
    
    long characterCount = 0;
    
    while (inFile.good()) {
        
        characterCount++;
        
        inFile >> value;
        
        cout << value;
        
    }
    
    if(inFile.eof())
    {
        cout << "文件读取结束" << endl;
    }
    else if (inFile.fail())
    {
        cout << "读取错误" << endl;
    }
    else
    {
        cout << "未知错误" << endl;
    }
    
    if(characterCount>0)
    {
        cout << "一共" << characterCount << "个字符" << endl;
    }
     */
    
    
    string fileName = "/Users/zhaoshijie/Desktop/c_learning_practice_demo/第6章/章节末编程练习/Practice/Practice/file.txt";

    ifstream inFile ;

    inFile.open(fileName);
    
    if(!inFile.is_open())
    {
        cout << "不能打开文件" << endl;
        
        exit(EXIT_FAILURE);
    }
    

//    string value[100] ;
    
    vector<string> value;

    int count = 0;

    while (inFile.good()) {

//        inFile >> value[count];
        
        string valueSteing ;
        
        getline(inFile, valueSteing);
        
//        getline(inFile, value[count]);
        
//        value[count] = valueSteing;
        
        value.push_back(valueSteing);

        count++;

    }

    if(inFile.eof())
    {
        cout << "文件读取结束" << endl;
    }
    else if (inFile.fail())
    {
        cout << "读取错误" << endl;
    }
    else
    {
        cout << "未知错误" << endl;
    }
    
    
    
    int size = (int)value.size();
    
    
    vector<Donation> donationList;

    cout << "输入内容" << endl;
    

    for (int i=0; i<(size - 1)/2.0; i++) {
        
        Donation donation ;
        
        for (int j=0; j<2; j++) {
            
            int index = i*2 + j;
            
            
            if(j == 0)
            {
                
                string money = value[index];
                
                if(money == "")
                {
                    break;
                }
                
                donation.money = stoi(money);
                
            }
            else if (j == 1)
            {
                
                
                if(value[index] == "")
                {
                    break;
                }
                
                strcpy(donation.name, value[index].c_str());
                
            }
            
        }
        
        donationList.push_back(donation);
        
    }

    
    
    cout << "Grand Patrons" << endl;
    
    int total = 0;
    
    for (int i=0; i<donationList.size(); i++)
    {
        Donation donation = donationList[i];
        

        if(donation.money>1000)
        {
            cout << "name:" << donation.name << " money:" << donation.money << endl;

            total++;
        }
        
        
    }
    

    cout << "Patrons" << endl;


    for (int i=0; i<donationList.size(); i++)
    {

        Donation donation = donationList[i];

        if(donation.money>0 && donation.money<1000)
        {
            cout << "name:" << donation.name << " money:" << donation.money << endl;

            total++;
        }

    }

    if(count == 0)
    {
        cout << "none" << endl;
    }
    
    
    
    
    return 0;
}
